MODULE debugprint 
  USE GLOBVAR
  use COMFUNCS
  USE INTERPOL, only : ARRAY1D_INIT

  IMPLICIT NONE

CONTAINS

  !output print
  subroutine printvalues(fid_)
     !Print out results
     implicit none
     integer, intent(in) :: fid_
     integer :: iRetired, itemp, it, it_pos
     real(8) :: rLS, rZA, rV
     character(100) :: filename
     
     write(filename, "(A40,I1,A4)") "/srv/scratch/BP_MPI/txt_vpgrid_",fid_,".txt"
     open(20,file=trim(filename))
     
     do it = N_T, N_0, -1  !period
       it_pos = it -N_0 + 1  
       do giH = N_H, 1, -1
          gH = sgridH%a(it, giH) !human capital
          do giFS = 1, N_FS  !family status
             do giRECSS = sgridRECSS%d1,1,-1   
                iRetired = sgridRECSS%a(giRECSS)
                do giHEALTH = 1, N_HEALTH
                   do giAIME = VN_AIME(it), 1, -1
                      gAIME = sgridAIME%a(it_pos, giAIME) !AIME
                      do giA = VN_A(it), 1, -1  !Asset
                         gA = sgridA%a(it_pos,giA) ! Asset
                         !leisure = 0
                         !rLS = 1.0d0-dcI%a(giA, giAIME, gilnXI, giRECSS, giH, it_pos)
                         !line 1, last is gA
                         !line 2, last is dvV_EV
                         write(20,'(I2,A,I2,A,F12.6,A,I2,A,I2,A,I2,A,I2,A,I2,A,F10.6,A,I2,A,E20.10,  &
                               &  A,E20.10,A,E20.10,A,I10,A,E100.50,A,E100.50,  &
                               &  A,E20.10,A,I10,A,E100.50,A,E100.50)') it, &
                               &  ',', giH, ',',  sgridH%a(it_pos,giH), &
                               &  ',', giFS, ',', giRECSS, ',', sgridRECSS%a(giRECSS), &
                               &  ',', giHEALTH,  &
                               &  ',', giAIME, ',',  sgridAIME%a(it_pos,giAIME), & 
                               &  ',', giA, ',',  gA,  &
                               !  leisure = 0
                               &  ',', dcc(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dcI(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dciRECSS(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dvV(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dvV_EV%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               !  now leisure=1
                               &  ',', dcc(1)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dciRECSS(1)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dvV(1)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', depsilon(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos)
                         enddo !giA
                      enddo !giAIME              
                   enddo   !giHEALTH  
                enddo   !giRECSS
             enddo  !giFS
          enddo   !giH
       enddo    !it
       close(20)
  end subroutine printvalues
 
  
    !output print for debugging
  subroutine testAG_print(fid_)
     !Print out results
     implicit none
     integer, intent(in) :: fid_
     integer :: iRetired, itemp, it, it_pos
     real(8) :: rLS, rZA, rV
     character(100) :: filename
     
     write(filename, "(A47,I1,A6,I1,A2,I1,A4)") "/srv/scratch/BP_MPI/tAG0/txt_tAG_color", &
              &  color_mpi,"_newid",new_id_mpi,"_v",fid_,".txt"
     open(20,file=trim(filename))
     
     do it = N_T, max(N_0, git-1), -1  !period
       it_pos = it - N_0 + 1  
       do giH = N_H, 1, -1
          gH = sgridH%a(it_pos, giH) !human capital
          do giFS = 1, N_FS  !family status
             do giRECSS = sgridRECSS%d1,1,-1   
                iRetired = sgridRECSS%a(giRECSS)
                do giHEALTH = 1, N_HEALTH
                   do giAIME = VN_AIME(it), 1, -1
                      gAIME = sgridAIME%a(it_pos, giAIME) !AIME
                      do giA = VN_A(it), 1, -1  !Asset
                         gA = sgridA%a(it_pos,giA) ! Asset
                         !leisure = 0
                         !rLS = 1.0d0-dcI%a(giA, giAIME, gilnXI, giRECSS, giH, it)
                         !line 1, last is gA
                         !line 2, last is dvV_EV
                         write(20,'(I2,A,I2,A,F12.6,A,I2,A,I2,A,I2,A,I2,A,I2,A,F10.6,A,I2,A,E20.10,  &
                               &  A,E20.10,A,E20.10,A,I10,A,E100.50,A,E100.50,  &
                               &  A,E20.10,A,I10,A,E100.50,A,E100.50)') it, &
                               &  ',', giH, ',',  sgridH%a(it_pos,giH), &
                               &  ',', giFS, ',', giRECSS, ',', sgridRECSS%a(giRECSS), &
                               &  ',', giHEALTH,  &
                               &  ',', giAIME, ',',  sgridAIME%a(it_pos,giAIME), & 
                               &  ',', giA, ',',  gA,  &
                               !  leisure = 0
                               &  ',', dcc(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dcI(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dciRECSS(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dvV(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dvV_EV%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               !  now leisure=1
                               &  ',', dcc(1)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dciRECSS(1)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', dvV(1)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos), &
                               &  ',', depsilon(0)%a(giA, giAIME, giHEALTH, giRECSS, giFS, giH, it_pos)
                         enddo !giA
                      enddo !giAIME              
                    enddo   !giHEALTH  
                 enddo   !giRECSS
             enddo  !giFS
          enddo   !giH
       enddo    !it
     close(20)
  end subroutine testAG_print
  
END MODULE debugprint
